<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Article_model extends CI_Model {

	public function get_articles() {
		$data = $this->db->select('aid,title,cname,time')
				->from('article')
				->join('category', 'article.cid=category.cid')
				->order_by('aid', 'asc')
				->get()
				->result_array();

		return $data;
	}

	public function insert($data) {
		$aid = $this->db->insert('article', $data);
		return $aid;
	}

	/**
	 * 查看文章
	 * @param  [type] $aid [description]
	 * @return [type]      [description]
	 */
	public function get_article($aid) {
		$article = $this->db->get_where('article', array('aid' => $aid))
					->row_array();
		return $article;
	}
	/**
	 * 编辑文章
	 * @param  [type] $aid  [description]
	 * @param  [type] $data [description]
	 * @return [type]       [description]
	 */
	public function edit($aid, $data) {
		$this->db->update('article', $data, array('aid'=>$aid));
	}

	public function delete($aid) {
		$this->db->delete('article', array('aid'=>$aid));
	}

	/**
	 * 右側列表文章
	 * @return [type] [description]
	 */
	public function hot_list() {
		$data = $this->db->select('aid,title')
				->order_by('time','desc')
				->limit(10)
				->get('article')
				->result_array();
		return $data;
	}

	public function home_articles() {
		$data['art'] = $this->db->select('aid,title,thumb,info')
					->order_by('time', 'desc')
					->limit(2)
					->get_where('article', array('type'=>0))
					->result_array();
		$data['hot'] = $this->db->select('aid,title,thumb,info')
					->order_by('time', 'desc')
					->limit(2)
					->get_where('article', array('type'=>1))
					->result_array();
		return $data;					
	}

	/**
	 * 调去栏目下的文章
	 * @return [type] [description]
	 */
	public function category_article($cid) {
		$data = $this->db->select('aid,title,info,thumb')
					->order_by('time', 'desc')
					->get_where('article', array('cid'=>$cid))
					->result_array();
		return $data;					
	}

	public function aid_article($aid) {
		$data = $this->db->join('category','article.cid=category.cid')
				->get_where('article', array('aid'=>$aid))
				->row_array();
		return $data;
	}



}

/* End of file article_model.php */
/* Location: ./application/models/article_model.php */